#ifndef  WAVEWORKLAYER_GLOBALS
#define  WAVEWORKLAYER_GLOBALS
#include <includes.h>

typedef enum {
    WF_STATE_IDLE,
    WF_STATE_INIT,
    WF_STATE_RUNING,
    WF_STATE_ENDED
} WF_State;

static void ProcessWaveform(uint8_t workflow, WF_State state);
static void ProcessVolatility(uint8_t TempWorkflow, WF_State state);
static void ProcessSteadyVoltage(uint8_t TempWorkflow, WF_State state);
static void ProcessLowFreProte(uint8_t TempWorkflow, WF_State state);
static void ProcessStartStop(uint8_t TempWorkflow, WF_State state);
static void ProcessLoadUnLoad(uint8_t TempWorkflow, WF_State state);
static void ProcessBuildUp(uint8_t TempWorkflow, WF_State state);

typedef struct {
    uint8_t startFlag;
    uint8_t endFlag;
    WF_State state;
    void (*process)(uint8_t workflow, WF_State state);
} WorkflowHandler;

#define g_workflowHandlers_len  7 
static WorkflowHandler g_workflowHandlers[g_workflowHandlers_len] = {
    {WFWave_VolatilityCollect, WFWave_VolatilityCollectEnd, WF_STATE_IDLE, ProcessVolatility},
    {WFWave_SteadyVoltage, WFWave_SteadyVoltageEnd, WF_STATE_IDLE, ProcessSteadyVoltage},
    {WFWave_LowFreProte, WFWave_LowFreProteEnd, WF_STATE_IDLE, ProcessLowFreProte},
    {WFWave_StartStop, WFWave_StartStopEnd, WF_STATE_IDLE, ProcessStartStop},
    {WFWave_LoadUnLoad, WFWave_LoadUnLoadEnd, WF_STATE_IDLE, ProcessLoadUnLoad},
    {WFWave_BuildUpVoltage, WFWave_BuildUpVoltageEnd, WF_STATE_IDLE, ProcessBuildUp},
    {WFWave_Waveform, WFWave_WaveformEnd, WF_STATE_IDLE, ProcessWaveform}
};

/* 波形数据 */
static void ProcessWaveform(uint8_t TempWorkflow, WF_State state)
{
    static ENUM_Waveform_Sta WFWaveState=ENUM_Waveform_Sta_None;//波形状态
    uint32_t CurveDataBuff_1_DAR=0;
    uint32_t CurveDataBuff_2_DAR=0;
    uint32_t CurveDataBuff_3_DAR=0;
    uint32_t CurveDataBuff_4_DAR=0;
    uint32_t CurveDataBuff_5_DAR=0;
    uint8_t VoltDataRate=0;
    uint8_t status1 = 0x0;
    uint8_t status2 = 0x0;
    uint8_t status3 = 0x0;
    uint8_t status4 = 0x0;
    uint8_t status5 = 0x0;
    uint16_t Max_DataBuffAddr_Sum=0;
    uint16_t TemIndex=0;

    // 初始化
    if(state==WF_STATE_INIT)
    {
        WFWaveState = ENUM_Waveform_Sta_Init;
    }

    /* 波形频率 */
    DataRateIndex++;
    switch (TempWorkflow)
    {
        case WorkflowMode_BuildUpVoltage:
            VoltDataRate=BUILDPRESSURE_VOLTDATARATE;
            Max_DataBuffAddr_Sum = SRAM_EstaMagn_CurvePointMaxNum;    
            break;
        case WorkflowMode_LowFreProte:
            VoltDataRate=LOWFREPROTE_VOLTDATARATE;   
            Max_DataBuffAddr_Sum = SRAM_UnderFreq_CurvePointMaxNum;    
            break;
        case WorkflowMode_Volatility:
            VoltDataRate=VOLATILITY_VOLTDATARATE;  
            Max_DataBuffAddr_Sum = SRAM_Volatility_CurvePointMaxNum;    
            break;
        case WorkflowMode_ExtExcitation:
            VoltDataRate=EXTEXCITATION_VOLTDATARATE;   
            Max_DataBuffAddr_Sum = SRAM_CeilingExci_CurvePointMaxNum;    
            break;
        case WorkflowMode_StartStop:
            VoltDataRate=STARTSTOP_VOLTDATARATE;    
            Max_DataBuffAddr_Sum = SRAM_StartStop_CurvePointMaxNum;    

            break;
        case WorkflowMode_Tuning0APF:
        case WorkflowMode_Tuning08PF:
        case WorkflowMode_Tuning06A04PF:
            VoltDataRate=TUNING_VOLTDATARATE;    
            Max_DataBuffAddr_Sum = SRAM_Upload_CurvePointMaxNum;    
            break;
        case WorkflowMode_LoadUnLoad:
            VoltDataRate=LOADUNLOAD_VOLTDATARATE;    
            Max_DataBuffAddr_Sum = SRAM_SuddenUnload_CurvePointMaxNum;    
            break;
    }


    if(DataRateIndex<(1000/VoltDataRate/25) && state!=WF_STATE_ENDED)
        return;
    else
        DataRateIndex=0;


    // 赋值
    switch (TempWorkflow)
    {
        case WorkflowMode_BuildUpVoltage:
            CurveDataBuff_1_DAR = Addr_EstaMagn_GenVoltDataCurve;
            CurveDataBuff_2_DAR = Addr_EstaMagn_GenExci_VoltDataCurve;
            CurveDataBuff_3_DAR = Addr_EstaMagn_GenExci_CurrentDataCurve;
            CurveDataBuff_4_DAR = Addr_EstaMagn_PowerVoltDataCurve;
            
            // 三相平均相电压曲线 Y 轴数据(改为建压前)
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.BuildVoltData.BuildVoltBEF;
            // AVR输出的励磁电压数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
            // AVR输出的励磁电流数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
            // AVR电源电压曲线 Y 轴数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.BuildVoltData.BuildVoltAFT;
            break;
        case WorkflowMode_ExtExcitation:
            CurveDataBuff_1_DAR = Addr_CeilingExci_GenVoltDataCurve;
            CurveDataBuff_2_DAR = Addr_CeilingExci_GenExci_VoltDataCurve;
            CurveDataBuff_3_DAR = Addr_CeilingExci_GenExci_CurrentDataCurve;
            // 三相平均相电压
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // AVR输出的励磁电压数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
            // AVR输出的励磁电流数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
            break;
        case WorkflowMode_LowFreProte:
            CurveDataBuff_1_DAR = Addr_UnderFreq_GenVoltDataCurve;
            CurveDataBuff_2_DAR = Addr_UnderFreq_GenExci_VoltDataCurve;
            CurveDataBuff_3_DAR = Addr_UnderFreq_GenExci_CurrentDataCurve;
            CurveDataBuff_4_DAR = Addr_UnderFreq_GenFreqDataCurve;        
        
            // 三相平均相电压曲线 Y 轴数据
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // AVR输出的励磁电压数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
            // AVR输出的励磁电流数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
            // AVR电源电压曲线 Y 轴数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;

            break;    
        case WorkflowMode_Volatility:
            CurveDataBuff_1_DAR = Addr_Volatility_VoltDataCurve_U;
            CurveDataBuff_2_DAR = Addr_Volatility_VoltDataCurve_V;
            CurveDataBuff_3_DAR = Addr_Volatility_VoltDataCurve_W;
            CurveDataBuff_4_DAR = Addr_Volatility_VoltDataCurve;
            CurveDataBuff_5_DAR = Addr_Volatility_FreqDataCurve;

            // 800 个线路 1 电压波动 Y 轴数据
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
            // 800 个线路 2 电压波动 Y 轴数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
            // 800 个线路 3 电压波动 Y 轴数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
            // 800 个3相平均电压波动 Y 轴数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // 800 个频率波动率测试数据
            share_CurveDataBuff_5.dataBuff[share_CurveDataBuff_5.index]=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
            break;
        case WorkflowMode_Tuning0APF:
        case WorkflowMode_Tuning08PF:
        case WorkflowMode_Tuning06A04PF:
            CurveDataBuff_1_DAR = Addr_Upload_VoltDataCurve_U;
            CurveDataBuff_2_DAR = Addr_Upload_VoltDataCurve_V;
            CurveDataBuff_3_DAR = Addr_Upload_VoltDataCurve_W;
            CurveDataBuff_4_DAR = Addr_Upload_VoltDataCurve;
            CurveDataBuff_5_DAR = Addr_Upload_FreqDataCurve;
            // 800 个线路 1 电压波动 Y 轴数据
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
            // 800 个线路 2 电压波动 Y 轴数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
            // 800 个线路 3 电压波动 Y 轴数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
            // 800 个3相平均电压波动 Y 轴数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // 800 个频率波动率测试数据
            share_CurveDataBuff_5.dataBuff[share_CurveDataBuff_5.index]=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
            break;
        case WorkflowMode_LoadUnLoad:
            CurveDataBuff_1_DAR = Addr_SuddenUnload_VoltDataCurve_U;
            CurveDataBuff_2_DAR = Addr_SuddenUnload_VoltDataCurve_V;
            CurveDataBuff_3_DAR = Addr_SuddenUnload_VoltDataCurve_W;
            CurveDataBuff_4_DAR = Addr_SuddenUnload_VoltDataCurve;
            CurveDataBuff_5_DAR = Addr_SuddenUnload_FreqDataCurve;
            // 800 个线路 1 电压波动 Y 轴数据
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
            // 800 个线路 2 电压波动 Y 轴数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
            // 800 个线路 3 电压波动 Y 轴数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
            // 800 个3相平均电压波动 Y 轴数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // 800 个频率波动率测试数据
            share_CurveDataBuff_5.dataBuff[share_CurveDataBuff_5.index]=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
            break;
        case WorkflowMode_StartStop:
            CurveDataBuff_1_DAR = Addr_StartStop_GenVoltCurve;
            CurveDataBuff_2_DAR = Addr_StartStop_ExciVoltCurve;
            CurveDataBuff_3_DAR = Addr_StartStop_ExciCurrentCurve;
            CurveDataBuff_4_DAR = Addr_StartStop_GenFreqCurve;

            // 三相平均相电压曲线 Y 轴数据
            share_CurveDataBuff_1.dataBuff[share_CurveDataBuff_1.index]=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
            // AVR输出的励磁电压数据
            share_CurveDataBuff_2.dataBuff[share_CurveDataBuff_2.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
            // AVR输出的励磁电流数据
            share_CurveDataBuff_3.dataBuff[share_CurveDataBuff_3.index]=ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
            // 频率 Y 轴曲线数据
            share_CurveDataBuff_4.dataBuff[share_CurveDataBuff_4.index]=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
            break;
    }

    
    switch (WFWaveState)
    {
    case ENUM_Waveform_Sta_None:
        break;
    case ENUM_Waveform_Sta_Init:
        share_DataBuffAddr_Sum = 0;
        DataRateIndex = 0;
        share_CurveDataBuff_1.index=0;
        share_CurveDataBuff_2.index=0;
        share_CurveDataBuff_3.index=0;
        share_CurveDataBuff_4.index=0;
        share_CurveDataBuff_5.index=0;

        WFWaveState=ENUM_Waveform_Sta_LessThanDataBuff;
        break;
    case ENUM_Waveform_Sta_LessThanDataBuff:
        if(state == WF_STATE_ENDED)
        {
            // 外面已经改变状态，无法下一次进入
            goto GT_WF_STATE_ENDED;
            break;
        }
        if(share_CurveDataBuff_1.index>=DataBuff_ST-1)
        {
            
            WFWaveState=ENUM_Waveform_Sta_GreThanDataBuff;
            break;
        }
        share_CurveDataBuff_1.index++;
        share_CurveDataBuff_2.index++;
        share_CurveDataBuff_3.index++;
        if(CurveDataBuff_4_DAR)
            share_CurveDataBuff_4.index++;
        if(CurveDataBuff_5_DAR)
            share_CurveDataBuff_5.index++;
        break;
    case ENUM_Waveform_Sta_GreThanDataBuff:   
GT_WF_STATE_ENDED:     
        if(share_DataBuffAddr_Sum + DataBuff_ST <= Max_DataBuffAddr_Sum)
        {
            if(state==WF_STATE_ENDED)
            {
                share_CurveDataBuff_1.index++;
                // 不够一次完整周期
                share_DataBuffAddr_Sum += share_CurveDataBuff_1.index;
                TemIndex = share_CurveDataBuff_1.index;
                WFWaveState=ENUM_Waveform_Sta_None;
            }
            else
            {
                share_DataBuffAddr_Sum += DataBuff_ST;
                TemIndex = DataBuff_ST;
                WFWaveState=ENUM_Waveform_Sta_LessThanDataBuff;
            }
        }
        else
        {
            WFWaveState=ENUM_Waveform_Sta_None;
            break;
        }

        status1 = DataBase_WriteDataContent(CurveDataBuff_1_DAR+(share_DataBuffAddr_Sum-TemIndex) * 2, (uint16_t *)share_CurveDataBuff_1.dataBuff, TemIndex * 2);
        status2 = DataBase_WriteDataContent(CurveDataBuff_2_DAR+(share_DataBuffAddr_Sum-TemIndex) * 2, (uint16_t *)share_CurveDataBuff_2.dataBuff, TemIndex * 2);
        status3 = DataBase_WriteDataContent(CurveDataBuff_3_DAR+(share_DataBuffAddr_Sum-TemIndex) * 2, (uint16_t *)share_CurveDataBuff_3.dataBuff, TemIndex * 2);
        
        if(CurveDataBuff_4_DAR)
        {
            status4 = DataBase_WriteDataContent(CurveDataBuff_4_DAR+(share_DataBuffAddr_Sum-TemIndex) * 2, (uint16_t *)share_CurveDataBuff_4.dataBuff, TemIndex * 2);
            share_CurveDataBuff_4.index=0;
        }
        if(CurveDataBuff_5_DAR)
        {
            status5 = DataBase_WriteDataContent(CurveDataBuff_5_DAR + (share_DataBuffAddr_Sum-TemIndex)*2, (uint16_t *)share_CurveDataBuff_5.dataBuff, TemIndex * 2);
            share_CurveDataBuff_5.index=0;
        }

        share_CurveDataBuff_1.index=0;
        share_CurveDataBuff_2.index=0;
        share_CurveDataBuff_3.index=0;
        break;
    }

    
}

static void ProcessVolatility(uint8_t TempWorkflow, WF_State state)
{
    static float LastMin_GenPhaseVoltL1=0;
    static float LastMin_GenPhaseVoltL2=0;
    static float LastMin_GenPhaseVoltL3=0;
    static float LastMin_GenPhaseVoltAverage;
    static float LastMin_GenFreqAverage=0;
    if(state==WF_STATE_INIT)
    {
        LastMin_GenPhaseVoltL1=0;
        LastMin_GenPhaseVoltL2=0;
        LastMin_GenPhaseVoltL3=0;
        LastMin_GenPhaseVoltAverage=0;
        LastMin_GenFreqAverage=0;

        TemSUM_GenPhaseVoltL1 = 0;
        TemSUM_GenPhaseVoltL2 = 0;
        TemSUM_GenPhaseVoltL3 = 0;
        TemSUM_GenPhaseVoltAverage = 0;

        TemMax_GenPhaseVoltL1 = 0;
        TemMax_GenPhaseVoltL2 = 0;
        TemMax_GenPhaseVoltL3 = 0;
        TemMax_GenPhaseVoltAverage = 0;

        TemMin_GenPhaseVoltL1 = 0;
        TemMin_GenPhaseVoltL2 = 0;
        TemMin_GenPhaseVoltL3 = 0;
        TemMin_GenPhaseVoltAverage = 0;

        TemMax_GenFreqAverage = 0;
        TemMin_GenFreqAverage = 0;
        TemSUM_GenFreqAverage = 0;

        TemSUM_ExcitationVolt = 0;
        TemSUM_ExcitationCurr = 0;
        TemSUM_ExcitationPower = 0;

        TemSUM_GenPowerP1 = 0;
        TemSUM_GenPowerP2 = 0;
        TemSUM_GenPowerP3 = 0;
        TemSUM_GenPowerQ1 = 0;
        TemSUM_GenPowerQ2 = 0;
        TemSUM_GenPowerQ3 = 0;
        TemSUM_GenPFL1 = 0;
        TemSUM_GenPFL2 = 0;
        TemSUM_GenPFL3 = 0;

        TemCount = 0;
        return;
    }
		
    /* 稳态 */
    TemSUM_GenPhaseVoltL1 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
    TemSUM_GenPhaseVoltL2 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
    TemSUM_GenPhaseVoltL3 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
    TemSUM_GenPhaseVoltAverage += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;

    /* 最大 */
    if(TemMax_GenPhaseVoltL1<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1)
        TemMax_GenPhaseVoltL1=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
    if(TemMax_GenPhaseVoltL2<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2)
        TemMax_GenPhaseVoltL2=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
    if(TemMax_GenPhaseVoltL3<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3)
        TemMax_GenPhaseVoltL3=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
    if(TemMax_GenPhaseVoltAverage<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage)
        TemMax_GenPhaseVoltAverage=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;

    /* 最小 */
    if(LastMin_GenPhaseVoltL1>ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1)
        TemMin_GenPhaseVoltL1=(double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
    if(LastMin_GenPhaseVoltL2>ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2)
        TemMin_GenPhaseVoltL2=(double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
    if(LastMin_GenPhaseVoltL3>ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3)
        TemMin_GenPhaseVoltL3=(double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
    if(LastMin_GenPhaseVoltAverage>ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage)
        TemMin_GenPhaseVoltAverage=(double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
    
    LastMin_GenPhaseVoltL1=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
    LastMin_GenPhaseVoltL2=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
    LastMin_GenPhaseVoltL3=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
    LastMin_GenPhaseVoltAverage=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;

    switch (TempWorkflow)
    {
    case WorkflowMode_Volatility:
        // 频率波动最大值
        if(TemMax_GenFreqAverage<ACAD_RTGlobalData.GenVoltData.GenFreqAverage)
            TemMax_GenFreqAverage=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
        // 频率波动最小值
        if(LastMin_GenFreqAverage>ACAD_RTGlobalData.GenVoltData.GenFreqAverage)
            TemMin_GenFreqAverage=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
        LastMin_GenFreqAverage = ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
        // 频率波动平均值
        TemSUM_GenFreqAverage+=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
        break;    
    case WorkflowMode_ExtExcitation:
        // 测试期间的AVR平均励磁电压（发电机）
        TemSUM_ExcitationVolt+=(double)ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
        // 测试期间的AVR平均励磁电流（发电机）
        TemSUM_ExcitationCurr+=(double)ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
        // 测试期间的AVR的平均励磁功率（发电机）
        TemSUM_ExcitationPower+=(double)ACAD_RTGlobalData.GenExcitationData.ExcitationPower;
        break;

    case WorkflowMode_Tuning0APF:
    case WorkflowMode_Tuning08PF: 
    case WorkflowMode_Tuning06A04PF:       
        TemSUM_GenPowerP1 += (double)ACAD_RTGlobalData.PowerData.GenPowerP1;//有功功率 P1
        TemSUM_GenPowerP2 += (double)ACAD_RTGlobalData.PowerData.GenPowerP2;//有功功率 P2
        TemSUM_GenPowerP3 += (double)ACAD_RTGlobalData.PowerData.GenPowerP3;//有功功率 P3
        TemSUM_GenPowerQ1 += (double)ACAD_RTGlobalData.PowerData.GenPowerQ1;//无功功率 Q1
        TemSUM_GenPowerQ2 += (double)ACAD_RTGlobalData.PowerData.GenPowerQ2;//无功功率 Q2
        TemSUM_GenPowerQ3 += (double)ACAD_RTGlobalData.PowerData.GenPowerQ3;//无功功率 Q3
        TemSUM_GenPFL1 += (double)ACAD_RTGlobalData.PowerData.GenPFL1;//功率因数 PF1
        TemSUM_GenPFL2 += (double)ACAD_RTGlobalData.PowerData.GenPFL2;//功率因数 PF2
        TemSUM_GenPFL3 += (double)ACAD_RTGlobalData.PowerData.GenPFL3;//功率因数 PF3
        TemSUM_ExcitationVolt += (double)ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;//整定期间的AVR平均励磁电压（发电机）
        TemSUM_ExcitationCurr += (double)ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;//整定期间的AVR平均励磁电流（发电机）
        TemSUM_ExcitationPower += (double)ACAD_RTGlobalData.GenExcitationData.ExcitationPower;//整定期间的AVR的平均励磁功率（发电机）

        break;
    }

    TemCount++;
		
}
/**
 * @brief 稳态
 * 
 * @param TempWorkflow 
 * @param state 
 */
static void ProcessSteadyVoltage(uint8_t TempWorkflow, WF_State state)
{
    if(state==WF_STATE_INIT)
    {
        TemSUM_GenPhaseVoltL1 = 0;
        TemSUM_GenPhaseVoltL2 = 0;
        TemSUM_GenPhaseVoltL3 = 0;
        TemSUM_GenPhaseVoltAverage = 0;
        TemCount = 0;
        return;
    }
    // 线路 1电压值
    TemSUM_GenPhaseVoltL1 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
    TemSUM_GenPhaseVoltL2 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
    TemSUM_GenPhaseVoltL3 += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
    TemSUM_GenPhaseVoltAverage += (double)ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
    // TemSUM_4 += (ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1+\
    //                         ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2+ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3)/3;
    TemCount++;
}
static void ProcessLowFreProte(uint8_t TempWorkflow, WF_State state)
{
    static uint8_t HRealityFre=DEF_FALSE;
    static uint8_t HRateRealityFre=DEF_FALSE;
    if(state==WF_STATE_INIT)
    {
        HRealityFre = DEF_FALSE;
        HRateRealityFre = DEF_FALSE;
        return;
    }

    if((ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage<share_UnderFreqData_st.baseData_st.SteadyAverageVolt*0.99)&&HRealityFre==DEF_FALSE)
    {
        HRealityFre=DEF_TRUE;
        share_UnderFreqData_st.baseData_st.RealityKneeFrequency=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;//实际拐点频率值
        // 低频拐点处线路 1 瞬态电压值
        // 低频拐点处线路 2  瞬态电压值
        // 低频拐点处线路 3  瞬态电压值
        // 低频拐点处的 瞬态三相平均电压值
        share_UnderFreqData_st.baseData_st.KneeFreqVolt_U=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;
        share_UnderFreqData_st.baseData_st.KneeFreqVolt_V=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;
        share_UnderFreqData_st.baseData_st.KneeFreqVolt_W=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;
        share_UnderFreqData_st.baseData_st.KneeFreqVoltage=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
    }

    if((ACAD_RTGlobalData.GenVoltData.GenFreqAverage<(share_UnderFreqData_st.baseData_st.RealityKneeFrequency-5)) && HRateRealityFre==DEF_FALSE && HRealityFre==DEF_TRUE)
    {
        // 低频保护期间的电压下降率/HZ
        share_UnderFreqData_st.baseData_st.VoltDropRate_KneeFreq=(share_UnderFreqData_st.baseData_st.KneeFreqVoltage-ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage)/5;
        HRateRealityFre=DEF_TRUE;
    }
}
static void ProcessStartStop(uint8_t TempWorkflow, WF_State state)
{
    if(state==WF_STATE_INIT)
    {
        TemMax_ExcitationVolt = 0;
        TemMax_GenPhaseVoltAverage = 0;
        TemMax_GenFreqAverage = 0;
        TemMax_ExcitationCurr = 0;
        return;
    }

    if(TemMax_ExcitationVolt<ACAD_RTGlobalData.GenExcitationData.ExcitationVolt)
    {
        // 最高励磁电压时的发电电压值
        TemMax_GenPhaseVoltAverage=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage;
        // 最高励磁电压时的发电频率值
        TemMax_GenFreqAverage=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;
        // 最高励磁电压时的励磁电压值
        TemMax_ExcitationVolt=ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
        // 最高励磁电压时的励磁电流值
        TemMax_ExcitationCurr=ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
    }
}
static void ProcessLoadUnLoad(uint8_t TempWorkflow, WF_State state)
{
    if(state==WF_STATE_INIT)
    { 
        TimerCount = 0;
        TemMax_GenPhaseVoltL1 = 0;
        TemMax_GenPhaseVoltL2 = 0;
        TemMax_GenPhaseVoltL3 = 0;
        TemMax_GenFreqAverage = 0;
        TemSUM_GenFreqAverage = 0;
        TemCount = 0;
        return;
    }

    // 25ms一次
    TimerCount+=25;
    /* 最大 */
    if(TemMax_GenPhaseVoltL1<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1)
        TemMax_GenPhaseVoltL1=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1;//测试期间的U相最高电压值
    if(TemMax_GenPhaseVoltL2<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2)
        TemMax_GenPhaseVoltL2=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2;//测试期间的V相最高电压值
    if(TemMax_GenPhaseVoltL3<ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3)
        TemMax_GenPhaseVoltL3=ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3;//测试期间的W相最高电压值
    if(TemMax_GenFreqAverage<ACAD_RTGlobalData.GenVoltData.GenFreqAverage)
        TemMax_GenFreqAverage=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;//突卸最高频率值
    TemSUM_GenFreqAverage+=ACAD_RTGlobalData.GenVoltData.GenFreqAverage;//稳态频率平均值
    TemCount++;

    if(share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_U==0 && ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL1>=(share_SuddenUnloadData_st.baseData_st.PhaseVoltAver_U * 0.99))
    {
        share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_U = TimerCount/1000.0;
    }
    if(share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_V==0 && ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL2>=(share_SuddenUnloadData_st.baseData_st.PhaseVoltAver_V * 1))
    {
        share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_V = TimerCount/1000.0;
    }
    if(share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_W==0 && ACAD_RTGlobalData.GenVoltData.GenPhaseVoltL3>=(share_SuddenUnloadData_st.baseData_st.PhaseVoltAver_W * 1.01))
    {
        share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime_W = TimerCount/1000.0;
    }
    if(share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime==0 && ACAD_RTGlobalData.GenVoltData.GenPhaseVoltAverage>=(share_SuddenUnloadData_st.baseData_st.PhaseVoltAver_W * 1.02))
    {
        share_SuddenUnloadData_st.baseData_st.UnloadRecoverTime = TimerCount/1000.0;
    }
}
static void ProcessBuildUp(uint8_t TempWorkflow, WF_State state)
{
    if(state==WF_STATE_INIT)
    { 
        TemSUM_ExcitationVolt = 0;
        TemSUM_ExcitationCurr = 0;
        TemCount = 0;
        return;
    }

    TemSUM_ExcitationVolt += ACAD_RTGlobalData.GenExcitationData.ExcitationVolt;
    TemSUM_ExcitationCurr += ACAD_RTGlobalData.GenExcitationData.ExcitationCurr;
    TemCount++;
}


uint8_t WaveRequestWorkLayerHandle(uint8_t TempWorkflow, uint8_t *ActInform) 
{
    uint8_t WFAction = DEF_FALSE;
    /* AlarmShutDown     */
    if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))
    {
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_VolatilityCollectEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_SteadyVoltageEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_LowFreProteEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_StartStopEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_LoadUnLoadEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_BuildUpVoltageEnd,BitOperaOpt_Set);
		ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
    }
    for (uint8_t i = 0; i < g_workflowHandlers_len; i++) {
        WorkflowHandler* handler = &g_workflowHandlers[i];

        switch (handler->state)
        {
        case WF_STATE_IDLE:
            if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_WFWave, handler->startFlag))
                handler->state = WF_STATE_INIT;
            break;
        case WF_STATE_INIT:
            handler->process(TempWorkflow, WF_STATE_INIT);
            *ActInform |= WorkflowOutRequestBit_WaveRequest;
            handler->state = WF_STATE_RUNING;
            break;
        case WF_STATE_RUNING:
            // 检查结束标志
            if (GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_WFWave, handler->endFlag)) 
            {
                ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_WFWave,handler->startFlag, BitOperaOpt_Clr);
                ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_WFWave,handler->endFlag, BitOperaOpt_Clr);
                handler->state = WF_STATE_ENDED;
            }
            handler->process(TempWorkflow, handler->state);
            break;
        case WF_STATE_ENDED:
            handler->state = WF_STATE_IDLE;
            break;
        }
        
    }
    return WFAction;
}


/**
 * @brief 结束流程通知发送
 * 
 * @param TempWorkflow 
 */
void WaveSetFlowDataStatus(uint8_t TempWorkflow)
{
    // 波形频率
    switch (TempWorkflow)
    {
        case WorkflowMode_BuildUpVoltage:
            // 设置sram点数
            share_EstaMagnData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_EstaMagnData_st.baseData_st.VoltDataRate=BUILDPRESSURE_VOLTDATARATE;    
            break;
        case WorkflowMode_LowFreProte:
            share_UnderFreqData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_UnderFreqData_st.baseData_st.VoltDataRate=LOWFREPROTE_VOLTDATARATE;   
            break;
        case WorkflowMode_Volatility:
            share_VolatilityData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_VolatilityData_st.baseData_st.VoltDataRate=VOLATILITY_VOLTDATARATE;  
            break;
        case WorkflowMode_ExtExcitation:
            share_CeilingExciData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_CeilingExciData_st.baseData_st.VoltDataRate=EXTEXCITATION_VOLTDATARATE;     
            break;
        case WorkflowMode_StartStop:
            share_StartStopData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_StartStopData_st.baseData_st.VoltDataRate=STARTSTOP_VOLTDATARATE;    
            break;
        case WorkflowMode_Tuning0APF:
        case WorkflowMode_Tuning08PF:
        case WorkflowMode_Tuning06A04PF:
            share_UploadData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_UploadData_st.baseData_st.VoltDataRate=TUNING_VOLTDATARATE;    
            break;
        case WorkflowMode_LoadUnLoad:
            share_SuddenUnloadData_st.baseData_st.VoltDataNumber=share_DataBuffAddr_Sum;
            share_SuddenUnloadData_st.baseData_st.VoltDataRate=LOADUNLOAD_VOLTDATARATE;    
            break;
    }

    /* 通知完成 */
    NetWork_SetTestFisishFlag(TempWorkflow);


}



#endif